package com.atguigu.springmvc.handlers;

import java.io.IOException;
import java.io.Writer;
import java.rmi.server.ObjID;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.atguigu.springmvc.entities.User;

/**
 * @author Administrator 1@RequestMapping
 */
// @SessionAttributes(value={"user"},types={String.class})//user和TestSessionAttributes方法中user一致，既放到了请求域中也放到了session中了。
@RequestMapping("/springmvc")
@Controller
public class SpringMVCTest {

	private static final String SUCCESS = "success";

	@RequestMapping("/testRequstMapping")
	public String TestSpringMVCTest() {
		System.out.println("test RequstMapping");
		return "success";
	}

	@RequestMapping(value = "/testMethod", method = RequestMethod.POST)
	public String TestMethod() {
		System.out.println("Test Method");
		return SUCCESS;
	}

	@RequestMapping(value = "/testParamsAndHeads", params = { "username", "age!=10" }, headers = {})
	public String TestParamsAndHeads() {
		System.out.println("Test Params and Heads");
		return SUCCESS;
	}

	@RequestMapping("/testAntPath/*/abc")
	public String TestAntPath() {
		System.out.println("test AntPath");
		return SUCCESS;
	}

	@RequestMapping("/testPathVariable/{id}")
	public String TestPathVariable(@PathVariable("id") Integer id) {
		System.out.println("test PathVariable: " + id);
		return SUCCESS;
	}

	// GET
	@RequestMapping(value = "/testRest/{id}", method = RequestMethod.GET)
	public String TestRest(@PathVariable("id") Integer id) {
		System.out.println("test Rest GET: " + id);
		return SUCCESS;

	}

	// POST
	@RequestMapping(value = "/testRest", method = RequestMethod.POST)
	public String TestRest() {
		System.out.println("test Rest POST");
		return SUCCESS;

	}

	// DELETE
	@RequestMapping(value = "/testRest/{id}", method = RequestMethod.DELETE)
	public String TestRestDelete(@PathVariable("id") Integer id) {
		System.out.println("test Rest DELETE: " + id);
		return SUCCESS;
	}

	// PUT
	@RequestMapping(value = "/testRest/{id}", method = RequestMethod.PUT)
	public String TestRestPut(@PathVariable("id") Integer id) {
		System.out.println("test Rest PUT: " + id);
		return SUCCESS;
	}

	// test RequesParam
	/**
	 * @RequestParam
	 * @param un
	 * @param age
	 * @return
	 */
	@RequestMapping("/testRequestPrama")
	public String TestRequestParam(@RequestParam(value = "userName") String un,
			@RequestParam(value = "age", required = false, defaultValue = "0") int age) {
		System.out.println("test RequestParam: userName: " + un + ",age: " + age);
		return SUCCESS;
	}

	// test RequestHeader
	/**
	 * 
	 * @param al
	 * @return
	 */
	@RequestMapping("/testRequestHeader")
	public String TestRequestHeader(@RequestHeader(value = "Accept-Language") String al) {
		System.out.println("test RequestHeader: Accept-Language " + al);
		return SUCCESS;
	}

	// test CookieValue
	/**
	 * 
	 * @param sessionId
	 * @return
	 */
	@RequestMapping("/testCookieValue")
	public String TestCookieValue(@CookieValue("JSESSIONID") String sessionId) {
		System.out.println("test CookieValue: " + sessionId);
		return SUCCESS;
	}

	// test Pojo
	/**
	 * springmvc
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping("/testPojo")
	public String TestPojo(User user) {
		System.out.println("user: " + user);
		return SUCCESS;
	}

	// test ServletAPI
	/**
	 * HttpServletRequest HttpServletResponse HttpSession
	 * java.security.Principal Locale InputStream OutputStream Reader Write
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/testServletAPI")
	public void TestServletAPI(HttpServletRequest request, HttpServletResponse response, Writer out)
			throws IOException {
		System.out.println("testServletAPI:" + request + "," + response);
		out.write("hello springmvc");
		// return SUCCESS;
	}

	// test moderandview
	@RequestMapping("/testModerAndView")
	public ModelAndView TestModerAndView() {
		String viewname = SUCCESS;
		ModelAndView modelAndView = new ModelAndView(viewname);

		modelAndView.addObject("time", new Date());
		return modelAndView;
	}

	// test map
	@RequestMapping("/testMap")
	public String TestMap(Map<String, Object> map) {
		System.out.println(map.getClass().getName());
		map.put("names", Arrays.asList("tom", "cat", "mike"));
		return SUCCESS;
	}

	// test sessionAttributes

	@RequestMapping("/testSessionAttributes")
	public String TestSessionAttributes(Map<String, Object> map) {
		User user = new User("tom", "123456", "tom@atguigu.com", 15);
		map.put("user", user);
		map.put("school", "atguigu");
		return SUCCESS;
	}

	@ModelAttribute
	private void getUser(@RequestParam(value = "id", required = false) Integer id, Map<String, Object> map) {
		// TODO Auto-generated method stub
		System.out.println("modeattibute method");
		if (id != null) {
			User user = new User(1, "tom", "123456", "tom@atguigu.com", 12);
			System.out.println("从数据库中获取一个对象：" + user);
			map.put("user", user);
		}
	}

	// 1.执行@ModelAttribute，从DB中取出对象，把对象放入到Map中，键位user
	// 2.springmvc从Map中取出User对象，并把表单的请求参数赋给该User对象的对应属性
	// 3.springmvc把上述对象传入目标方法的参数
	// 4.note map.put("user" user),引号中键需要和目标方法入参类型的第一个字母小写字符串一致
	// test ModelAttribute
	@RequestMapping("/testModelAttribute")
	public String TestModelAttribute(@ModelAttribute("user") User user) {
		System.out.println("修改:" + user);
		return SUCCESS;
	}
}
